package com.leetcode;

import java.util.ArrayList;
import java.util.List;

public class No102 {

    public List<List<Integer>> levelOrder(TreeNode root) {
        if (root == null) return new ArrayList<>();
        List<List<Integer>> res = new ArrayList<>();

        List<TreeNode> level = new ArrayList<>();
        level.add(root);
        while (!level.isEmpty()) {
            List<TreeNode> temp = new ArrayList<>();
            List<Integer> tempVal = new ArrayList<>();
            for (TreeNode node : level) {
                if (node.left != null) {
                    temp.add(node.left);
                }
                if (node.right != null) {
                    temp.add(node.right);
                }
                tempVal.add(node.val);
            }
            level = temp;
            res.add(tempVal);
        }
        return res;
    }
}
